package com.datangnet.parking.basecommon.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class FuncUtils {

	/**
	 * 生成随机字符串. 类型为仅数字,即0~9
	 */
	public static final int TYPE_NUM_ONLY = 0;

	/**
	 * 生成随机字符串. 类型为仅字母,即大小写字母混合
	 */
	public static final int TYPE_LETTER_ONLY = 1;

	/**
	 * 生成随机字符串. 类型为数字和大小写字母混合
	 */
	public static final int TYPE_ALL_MIXED = 2;

	/**
	 * 生成随机字符串. 类型为数字和大写字母混合
	 */
	public static final int TYPE_NUM_UPPER = 3;

	/**
	 * 生成随机字符串. 类型为数字和小写字母混合
	 */
	public static final int TYPE_NUM_LOWER = 4;

	/**
	 * 生成随机字符串. 类型为仅大写字母
	 */
	public static final int TYPE_UPPER_ONLY = 5;

	/**
	 * 生成随机字符串. 类型为仅小写字母
	 */
	public static final int TYPE_LOWER_ONLY = 6;

	/**
	 * 生成随机字符串.
	 * 
	 * @param type
	 *            类型,参见本类的静态属性
	 * @param length
	 *            长度,要求大于0的整数
	 * @param excludeString
	 *            需排除的特殊字符（无需排除则为null）
	 * @return 随机字符串
	 */
	public static String generateTextCode(int type, int length,
			String excludeString) {
		if (length <= 0) {
			return "";
		}
		StringBuffer verifyCode = new StringBuffer();
		int i = 0;
		Random random = new Random();
		switch (type) {
		case TYPE_NUM_ONLY:
			while (i < length) {
				int t = random.nextInt(10);
				// 排除特殊字符
				if (null == excludeString || excludeString.indexOf(t + "") < 0) {
					verifyCode.append(t);
					i++;
				}
			}
			break;
		case TYPE_LETTER_ONLY:
			while (i < length) {
				int t = random.nextInt(123);
				if ((t >= 97 || (t >= 65 && t <= 90))
						&& (null == excludeString || excludeString
								.indexOf((char) t) < 0)) {
					verifyCode.append((char) t);
					i++;
				}
			}
			break;
		case TYPE_ALL_MIXED:
			while (i < length) {
				int t = random.nextInt(123);
				if ((t >= 97 || (t >= 65 && t <= 90) || (t >= 48 && t <= 57))
						&& (null == excludeString || excludeString
								.indexOf((char) t) < 0)) {
					verifyCode.append((char) t);
					i++;
				}
			}
			break;
		case TYPE_NUM_UPPER:
			while (i < length) {
				int t = random.nextInt(91);
				if ((t >= 65 || (t >= 48 && t <= 57))
						&& (null == excludeString || excludeString
								.indexOf((char) t) < 0)) {
					verifyCode.append((char) t);
					i++;
				}
			}
			break;
		case TYPE_NUM_LOWER:
			while (i < length) {
				int t = random.nextInt(123);
				if ((t >= 97 || (t >= 48 && t <= 57))
						&& (null == excludeString || excludeString
								.indexOf((char) t) < 0)) {
					verifyCode.append((char) t);
					i++;
				}
			}
			break;
		case TYPE_UPPER_ONLY:
			while (i < length) {
				int t = random.nextInt(91);
				if ((t >= 65)
						&& (null == excludeString || excludeString
								.indexOf((char) t) < 0)) {
					verifyCode.append((char) t);
					i++;
				}
			}
			break;
		case TYPE_LOWER_ONLY:
			while (i < length) {
				int t = random.nextInt(123);
				if ((t >= 97)
						&& (null == excludeString || excludeString
								.indexOf((char) t) < 0)) {
					verifyCode.append((char) t);
					i++;
				}
			}
			break;
		}
		return verifyCode.toString();
	}

	public static <T> List<T[]> split(T[] array, int number) {
		if (array == null || array.length < 1 || number < 1) {
			return null;
		}

		List<T[]> list = new ArrayList<T[]>();
		int index = 0;
		while (index < array.length) {
			if (index + number <= array.length) {
				list.add(Arrays.copyOfRange(array, index, index + number));
				index = index + number;
			} else {
				list.add(Arrays.copyOfRange(array, index, array.length));
				break;
			}
		}

		return list;
	}

	public static void main(String[] args) {
		System.out.println(split(new String[] { "1", "2", "3", "4", "5" }, 2));
	}
}
